#include "/home/zhong/muduo/muduo/net/TcpServer.h" // 我的muduo库放在~目录下了
#include "/home/zhong/muduo/muduo/net/EventLoop.h"
#include "/home/zhong/muduo/muduo/net/TcpConnection.h"
#include "/home/zhong/muduo/muduo/base/Logging.h"
// muduo库内实现的结合protobuf协议框架
#include "/home/zhong/muduo/examples/protobuf/codec/dispatcher.h"
#include "/home/zhong/muduo/examples/protobuf/codec/codec.h"

#include "request.pb.h"
#include <iostream>
#include <string>
#include <unordered_map>
#include <functional>

class Server
{
public:
    typedef std::shared_ptr<zhong::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<zhong::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<zhong::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<zhong::AddResponse> AddResponsePtr;

    Server(int port)
        // 实现TcpServer 函数分发器 函数处理器的初始化
        : _server(&_base_loop, muduo::net::InetAddress("0.0.0.0", port), "Server", muduo::net::TcpServer::kNoReusePort),
          _dispatcher(std::bind(&Server::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 1.注册业务请求处理函数
        _dispatcher.registerMessageCallback<zhong::TranslateRequest>(std::bind(&Server::onTranslate,
                                                                               this,
                                                                               std::placeholders::_1,
                                                                               std::placeholders::_2,
                                                                               std::placeholders::_3));
        _dispatcher.registerMessageCallback<zhong::AddRequest>(std::bind(&Server::onAdd,
                                                                         this,
                                                                         std::placeholders::_1,
                                                                         std::placeholders::_2,
                                                                         std::placeholders::_3));

        // 2.设置消息回调函数
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _server.setConnectionCallback(std::bind(&Server::onConnection, this, std::placeholders::_1));
    }

    void start()
    {
        _server.start();
        _base_loop.loop();
    }

private:
    std::string Translate(const std::string str)
    {
        std::unordered_map<std::string, std::string> dict_map;
        dict_map.insert(std::make_pair("apple", "苹果"));
        dict_map.insert(std::make_pair("happy", "开心"));
        dict_map.insert(std::make_pair("hello", "你好"));
        auto it = dict_map.find(str);
        if (it != dict_map.end())
        {
            return it->second;
        }
        else
        {
            return "no find this word Chinese meaning";
        }
    }
    // 消息处理相关函数
    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateRequestPtr &message, muduo::Timestamp)
    {
        // 1.提取请求
        std::string request = message->msg();
        // 2.进行翻译
        std::string response = Translate(request);
        // 3.实现序列化
        zhong::TranslateResponse resp;
        resp.set_msg(response);
        // 4.发送响应
        _codec.send(conn, resp);
    }
    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;
        zhong::AddResponse resp;
        resp.set_result(result);
        _codec.send(conn, resp);
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
    }
    // 链接处理相关函数
    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected() == true)
        {
            LOG_INFO << "new connection build"; // log_
        }
        else
        {
            LOG_INFO << "connection quit";
        }
    }

private:
    muduo::net::TcpServer _server;
    muduo::net::EventLoop _base_loop;
    ProtobufDispatcher _dispatcher; // 请求分发器
    ProtobufCodec _codec;           //
};

int main()
{
    std::unique_ptr<Server> server = std::make_unique<Server>(8080);
    server->start();
}